# coding=UTF-8
__author__ = 'Administrator'
from readData import *
import os


def make_team_graph():
    file1 = os.listdir(r'.\playerInfo\wangyi')
    file3 = open(u'.\\knowledgeGraph\\' + 'player.csv', mode='a')
    file4 = open(u'.\\knowledgeGraph\\' + 'team2player.csv', mode='a')
    for f in file1:
        # if '华盛顿奇才' in f.decode('gbk').encode('utf-8'):
        team_name = f.decode('gbk')[9:-8].encode('utf-8') + '队'
        file2 = open('.\\playerInfo\\wangyi\\' + f)
        relation = file2.next().split(' ')
        for l in file2:
            info = l.split(' ')
            file4.write(team_name + ' ' + '现役球员' + ' ' + info[1] + '\n')
            if info[0] != '':
                file3.write(info[1] + ' ' + relation[0] + ' ' + info[0] + '\n')
            if info[2] != '':
                file3.write(info[1] + ' ' + relation[2] + ' ' + info[2] + '\n')
            if info[3] != '':
                file3.write(info[1] + ' ' + relation[3] + ' ' + info[3] + '\n')

            if info[4] != '':
                file3.write(info[1] + ' ' + relation[4] + ' ' + info[4] + '\n')
            if info[5] != '':
                file3.write(info[1] + ' ' + relation[5] + ' ' + info[5] + '\n')
            if info[6] != '':
                file3.write(info[1] + ' ' + relation[6] + ' ' + info[6] + '\n')
            if info[7] != '':
                file3.write(info[1] + ' ' + relation[7] + ' ' + info[7] + '\n')
            if info[8] != '':
                file3.write(info[1] + ' ' + '球员主页' + ' ' + info[8].strip() + '\n')
        file2.close()
    file3.close()
    file4.close()


def make_team_graph2():
    file = open(r'.\team\team')
    relation = file.next().strip().split(' ')
    for l in file:
        info = l.strip().split(' ')
        team_name = info[0] + '队'
        file2 = open('.\\knowledgeGraph\\' + team_name.decode('utf-8'), mode='a')
        file2.write(team_name + ' ' + relation[1] + ' ' + info[1] + '\n')
        file2.write(team_name + ' ' + relation[2] + ' ' + info[2] + '\n')
        file2.write(team_name + ' ' + relation[3] + ' ' + info[3] + '\n')
        file2.write(team_name + ' ' + relation[4] + ' ' + info[4] + '\n')
        file2.write(team_name + ' ' + relation[5] + ' ' + info[5].strip() + '\n')
        file2.close()


def get_team_ename():
    file = codecs.open(r'.\team\team', encoding='utf-8')
    team_d = {}

    for f in file.readlines()[1:]:
        team_d[f.split(' ')[0]] = f.split(' ')[1]
    return team_d


def make_team2team_graph():
    team_d = get_team_ename()
    result = {}
    file = codecs.open(r'.\knowledgeGraph\team2team', mode='a', encoding='utf-8')
    for i in range(0, len(team_d)):
        for j in range(i + 1, len(team_d)):
            info = get_team_comparison(team_d[team_d.keys()[i]], team_d[team_d.keys()[j]], team_d.keys()[i],
                                       team_d.keys()[j], result)
            file.write(info + '\n')
    for k, v in result.iteritems():
        file2 = codecs.open('.\\knowledgeGraph\\' + k + u'战报', mode='a', encoding='utf-8')
        for c in v:
            file2.write(c + '\n')
        file2.close()
    file.close()
    a = 0


def join():
    file1 = os.listdir(r'.\knowledgeGraph\pt')
    file = open(r'.\knowledgeGraph\team2player', mode='a')
    for f in file1:
        file2 = open('.\\knowledgeGraph\\pt\\' + f)
        for line in file2:
            if '队' in line and '球员' in line:
                file.write(line)
        file2.close()
    file.close()


def getPoint():
    file = open('.\knowledgeGraph\gameInfo')
    file2 = open('.\knowledgeGraph\gameInfo2', mode='a')
    for l in file.readlines()[1:]:
        d = l.split(' ')
        p1 = int(d[6]) + int(d[7]) + int(d[8]) + int(d[9])
        p2 = int(d[10]) + int(d[11]) + int(d[12]) + int(d[13])
        file2.write(d[0] + ' ' + str(p1) + '-' + str(p2) + '\n')
    file.close()
    file2.close()


if __name__ == '__main__':
    team = ['老鹰队', '黄蜂队', '热火队', '魔术队', '奇才队', '公牛队', '骑士队', '活塞队', '步行者队', '雄鹿队', '篮网队', '凯尔特人队', '尼克斯队', '76人队',
            '猛龙队', '勇士队', '快船队', '湖人队', '太阳队', '国王队', '掘金队', '森林狼队', '雷霆队', '开拓者队', '爵士队', '小牛队', '火箭队', '灰熊队', '鹈鹕队',
            '马刺队']
    # cur_player_c, cur_player_e, all_player_c, all_player_e, id=loadplayer()
    # c_id=dict(zip(all_player_c, id))
    # e_id=dict(zip(all_player_e, id))
    # for name in cur_player_c:
    #     print c_id[name]
    # file=codecs.open(r'D:\data\baiketriples\baike_triples.txt',encoding='utf-8')
    # file2=codecs.open(r'.\knowledgeGraph\baike2',mode='a',encoding='utf-8')
    # for l in file:
    #     for t in team:
    #         if t in l.encode('utf-8'):
    #            file2.write(l)
    #            break
    # file.close()
    # file2.close()
    # make_team_graph()
    # make_team2team_graph()
    # make_team_graph()
    getPoint()
    a = 0
